/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.transas.zorlog.data.log;

import com.transas.zorlog.data.DataSource;
import com.transas.zorlog.data.NullDataSource;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;

/**
 *
 * @author amalikov
 */
public class LogLoaderWorker extends SwingWorker<DataSource, Void> {
	
	public LogLoaderWorker(String logFileName, String xmlFileName, String type) {
		if ("fcslog".equals(type))
			loader = new PacketLogLoader(logFileName, xmlFileName, PacketLogLoader.PacketType.FCS);
		else if ("gcslog".equals(type))
			loader = new PacketLogLoader(logFileName, xmlFileName, PacketLogLoader.PacketType.GCS);
		else if ("textlog".equals(type))
			loader = new TextLogLoader(logFileName, xmlFileName);
		else if ("binslog".equals(type))
			loader = new PacketLogLoader(logFileName, xmlFileName, PacketLogLoader.PacketType.BINS);
		else
			loader = null;
	}
	
	@Override
	protected DataSource doInBackground() throws Exception {
		try {
			return loader.read(new ProgressInformer(){
				@Override
				public void set(int value) {
					setProgress(value);
				}
			});
		} catch (IOException ex) {
			Logger.getLogger(PacketReader.class.getName()).log(Level.SEVERE, null, ex);
			return new NullDataSource();
		}
	}
	
	private LogLoader loader;
}
